using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Maestros_Usuarios : Global
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (UsrTipo(Context) == "ADMIN")
        {
            if (!IsPostBack)
                grdUsuarios.SelectedIndex = -1;

            sdsUsuario.Select(DataSourceSelectArguments.Empty);
        }
        else
        {
            Response.Redirect("../Bienvenido.aspx?ID=1", false);
        }
    }

  
    protected void btnGrabar_Click(object sender, EventArgs e)
    {
        try
        {
            if (grdUsuarios.SelectedIndex != -1)
                sdsUsuario.Update();
            else
                sdsUsuario.Insert();

            Limpiar();
        }
        catch(Exception EX)
        {
            oGJS.ScriptAlert("CEDIS - Ocurrio un error en el sistema",false, Page);
            oGDataBlocks.RegistraError(0, 0, "USUARIOS - Error 1: " + EX.Message);
        }

    }

    protected void grdUsuarios_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            this.txtNombre.Text = ((LinkButton)grdUsuarios.SelectedRow.FindControl("btnNombre")).Text;
            this.txtUsuario.Text = grdUsuarios.SelectedRow.Cells[2].Text;
            this.txtContrasena.TextMode = TextBoxMode.SingleLine;
            this.txtConContrasena.TextMode = TextBoxMode.SingleLine;
            this.txtContrasena.Text = grdUsuarios.SelectedRow.Cells[3].Text;
            this.txtConContrasena.Text = grdUsuarios.SelectedRow.Cells[3].Text;
            this.ddlTipo.SelectedValue = grdUsuarios.SelectedRow.Cells[4].Text;
            this.chkActivo.Checked = ((CheckBox)grdUsuarios.SelectedRow.FindControl("chkActivo")).Checked;
        }
        catch (Exception EX)
        {
            oGJS.ScriptAlert("CEDIS - Ocurrio un error en el sistema",false, Page);
            oGDataBlocks.RegistraError(0, 0, "USUARIOS - Error 2: " + EX.Message);
        }
    }

    private void Limpiar()
    {
        this.txtNombre.Text = string.Empty;
        this.txtUsuario.Text = string.Empty;
        this.chkActivo.Checked = false;
        this.ddlTipo.SelectedValue = "-1";
        this.grdUsuarios.SelectedIndex = -1;
        this.txtContrasena.TextMode = TextBoxMode.Password;
        this.txtConContrasena.TextMode = TextBoxMode.Password;
        this.txtContrasena.Text = string.Empty;
        this.txtConContrasena.Text = string.Empty;
    }
}

